package com.indexingsystem.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.TablesQueryVo;
import com.indexingsystem.boss.Vo.WebChatSharesVo;
import com.indexingsystem.boss.admin.mapper.ArticleContentsMapper;
import com.indexingsystem.boss.admin.mapper.ArticlesMapper;
import com.indexingsystem.boss.admin.mapper.AuditLogsMapper;
import com.indexingsystem.boss.admin.mapper.DhMagazinesMlMapper;
import com.indexingsystem.boss.admin.mapper.DhResourceauditlogsMapper;
import com.indexingsystem.boss.admin.mapper.ResCategoryRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourceRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourcesMapper;
import com.indexingsystem.boss.admin.mapper.SpecialResourceDatasMapper;
import com.indexingsystem.boss.admin.mapper.WechatSharesMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.ArticleContents;
import com.indexingsystem.boss.entity.Articles;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.ResourceRelations;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.entity.WechatShares;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.InstitutionLoadUtils;
import com.indexingsystem.db.mongoDB.impl.WeChatSharesMongo;
import com.indexingsystem.system.service.IWechatSharesService;
import com.indexingsystem.system.service.IdhTodayStorageInfosService;

@Service
@Transactional(rollbackFor = Throwable.class)
public class WechatSharesServiceImpl implements IWechatSharesService {

	@Resource
	private ArticlesMapper				articlesMapper;
	@Resource
	private ResourcesMapper				resourcesMapper;
	@Resource
	private ResCategoryRelationsMapper	resCategoryRelationsMapper;
	@Resource
	private SpecialResourceDatasMapper	specialResourceDatasMapper;

	@Resource
	private AuditLogsMapper				auditLogsMapper;

	@Resource
	private IdhTodayStorageInfosService	todayStorageInfosDao;

	@Resource
	private DhResourceauditlogsMapper	dhResourceauditlogsMapper;

	@Resource
	private ArticleContentsMapper		articleContentsMapper;

	@Resource
	private ResourceRelationsMapper		resourceRelationsMapper;

	@Resource
	private DhMagazinesMlMapper			dhMagazinesMlMapper;

	@Resource
	private WechatSharesMapper			wechatSharMapper;

	@Override
	public int deleteByPrimaryKey(String resId) {
		return wechatSharMapper.deleteByPrimaryKey( resId );
	}

	@Override
	public int insert(WechatShares record) {

		return wechatSharMapper.insert( record );
	}

	@Override
	public int insertSelective(WechatShares record) {

		return wechatSharMapper.insertSelective( record );
	}

	@Override
	public WechatShares selectByPrimaryKey(String resId) {
		return wechatSharMapper.selectByPrimaryKey( resId );
	}

	@Override
	public int updateByPrimaryKeySelective(WechatShares record) {
		return wechatSharMapper.updateByPrimaryKeySelective( record );
	}

	@Override
	public int updateByPrimaryKey(WechatShares record) {
		return wechatSharMapper.updateByPrimaryKey( record );
	}

	@Override
	public PagedResult<WebChatSharesVo> getWechatShardList(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();

		List<WebChatSharesVo> webChatSharesVos = WeChatSharesMongo.findWebChatShareslesByPage( webChatSharesVo,
				InstitutionLoadUtils.getString( "institutionId" ) );
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );

		if (null != webChatSharesVos && webChatSharesVos.size() > 0) {
			System.err.println( "缓存+++++++++++++++++++++++++++++++++++++++" );
			return BeanUtils.toPagedResult( webChatSharesVos );
		} else {
			System.err.println( "DB ----------------------------------" );
			return BeanUtils.toPagedResult( wechatSharMapper.getWechatShardList( webChatSharesVo ) );
		}
	}

	@Override
	public Integer getWechatShardListCount(WebChatSharesVo webChatSharesVo) {
		Integer maxCount = 0;
		maxCount = WeChatSharesMongo.findWebChatShareslesMaxCount( webChatSharesVo, InstitutionLoadUtils.getString( "institutionId" ) );
		if (maxCount == 0) {
			maxCount = wechatSharMapper.getWechatShardListCount( webChatSharesVo );
		}
		return maxCount;
	}

	@Override
	public WechatShares getWechatShardListCountByRefId(WebChatSharesVo webChatSharesVo) {
		// TODO Auto-generated method stub
		return wechatSharMapper.getWechatShardListCountByRefId( webChatSharesVo );
	}

	@Override
	public WechatShares getWechatShardModelByRefId(WebChatSharesVo webChatSharesVo) {
		return wechatSharMapper.getWechatShardModelByRefId( webChatSharesVo );
	}

	@Override
	public int deleteWechatSharesByIdBatch(List<String> resIdList) throws Exception {
		int result = 0;
		if (null != resIdList && resIdList.size() > 0) {
			for (String resId : resIdList) {
				WechatShares queryArticles = selectByPrimaryKey( resId );
				if (null != queryArticles) {
					// 1、删除
					result += deleteByPrimaryKey( queryArticles.getResId() );

					// 第三步删除资源关系
					// 资源关系列表
					List<ResourceRelations> queryResRelationsList = resourceRelationsMapper
							.getResourceRelationsListByResId( queryArticles.getResId() );
					for (ResourceRelations resRelations : queryResRelationsList) {
						resourceRelationsMapper.deleteByPrimaryKey( resRelations.getResRelationId() );
					}

					// 第四步：删除资源终端关系
					List<SpecialResourceDatas> tempSpecialResDataList = specialResourceDatasMapper.getSpecialResourceDataListByResId( queryArticles
							.getResId() );
					for (SpecialResourceDatas resSpecialDatas : tempSpecialResDataList) {
						specialResourceDatasMapper.deleteBySpecialResourceDataId( resSpecialDatas.getSpecialResDataId() );
					}

					// 5、删除审核日志表关系
					List<AuditLogs> auditLogsList = auditLogsMapper.getAuditLogsListByResId( queryArticles.getResId() );
					for (AuditLogs auditLogs : auditLogsList) {
						auditLogsMapper.deleteByPrimaryKey( auditLogs.getProductLogId() );
					}

					// 清理入库审核日志表关系
					DhResourceauditlogs dhResourceAuditLogs = dhResourceauditlogsMapper.getResAuditlogsByResId( queryArticles.getResId() );
					if (null != dhResourceAuditLogs) {
						dhResourceauditlogsMapper.deleteByPrimaryKey( dhResourceAuditLogs.getId() );
					}

					// 删除流水
					List<DhTodayStorageInfos> dhTodayStorageInfosList = todayStorageInfosDao.getDhTodayStorageInfosByResId( queryArticles.getResId() );
					for (DhTodayStorageInfos dhTodayStorageInfos : dhTodayStorageInfosList) {
						todayStorageInfosDao.deleteTodayStorageInfoById( dhTodayStorageInfos.getStorageId() );
					}

					resourcesMapper.deleteByPrimaryKey( queryArticles.getResId() );

				}

			}
			WeChatSharesMongo.deleteWeChatSharesByIds( resIdList, InstitutionLoadUtils.getString( "institutionId" ) );
		}

		return result;

	}

	@Override
	public PagedResult<WechatShares> getWechatShardModelByRefIdList(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( wechatSharMapper.getWechatShardModelByRefIdList( webChatSharesVo ) );
	}

	@Override
	public Integer getWechatShardModelByRefIdListChildCount(WebChatSharesVo webChatSharesVo) {
		return wechatSharMapper.getWechatShardModelByRefIdListChildCount( webChatSharesVo );
	}

	@Override
	public Integer getWechatShardModelByRefIdCountList(WebChatSharesVo webChatSharesVo) {
		return wechatSharMapper.getWechatShardModelByRefIdCountList( webChatSharesVo );
	}

	@Override
	public PagedResult<WechatShares> getWechatShardModelByRefIdListByChild(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( wechatSharMapper.getWechatShardModelByRefIdListByChild( webChatSharesVo ) );
	}

	@Override
	public Integer getWechatShardModelByRefIdListByChildCount(WebChatSharesVo webChatSharesVo) {
		return wechatSharMapper.getWechatShardModelByRefIdListByChildCount( webChatSharesVo );
	}

	@Override
	public PagedResult<WechatShares> getWechatShardModelByRefIdListByParent(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( wechatSharMapper.getWechatShardModelByRefIdListByParent( webChatSharesVo ) );
	}

	@Override
	public Integer getWechatShardModelByRefIdCountListByParent(WebChatSharesVo webChatSharesVo) {
		// TODO Auto-generated method stub
		return wechatSharMapper.getWechatShardModelByRefIdCountListByParent( webChatSharesVo );
	}

	@Override
	public Integer getWechatShardModelByRefIdCountListByChild(WebChatSharesVo webChatSharesVo) {
		// TODO Auto-generated method stub
		return wechatSharMapper.getWechatShardModelByRefIdCountListByChild( webChatSharesVo );
	}

	@Override
	public PagedResult<WechatShares> getWechatShardModelByRefIdListByChildGroupPeriodName(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( wechatSharMapper.getWechatShardModelByRefIdListByChildGroupPeriodName( webChatSharesVo ) );
	}

	@Override
	public Integer getWechatShardModelByRefIdListByChildGroupPeriodNameCount(WebChatSharesVo webChatSharesVo) {
		// TODO Auto-generated method stub
		return wechatSharMapper.getWechatShardModelByRefIdListByChildGroupPeriodNameCount( webChatSharesVo );
	}

	@Override
	public PagedResult<WechatShares> getPavilionRelationsForWebChatList(WebChatSharesVo webChatSharesVo) {
		Integer pageNo = webChatSharesVo.getPageNumber() == null ? 1 : webChatSharesVo.getPageNumber();
		Integer pageSize = webChatSharesVo.getPageSize() == null ? 10 : webChatSharesVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( wechatSharMapper.getPavilionRelationsForWebChatList( webChatSharesVo ) );
	}

	@Override
	public Integer getPavilionRelationsForWebChatCount(WebChatSharesVo webChatSharesVo) {
		// TODO Auto-generated method stub
		return wechatSharMapper.getPavilionRelationsForWebChatCount( webChatSharesVo );
	}
}